var commonUtil = require("../../../utils/commonUtil.js");
var globalData = getApp().globalData;
var httpsUrl = globalData.httpsUrl;
var imgUrl = globalData.imgUrl;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgUrl: imgUrl,
    phone: "",
    validateCode: "",//验证码
    flag: false,
    codeDis: false,//disabled
    phoneCode: "获取验证码",
  },
  updatePhone: function () {
    var params = new Object();
    params.phoneNo = this.data.phone;
    params.verifyCode = this.data.validateCode;
    if (commonUtil.isEmpty(params.phoneNo)) {
      commonUtil.showModal("请输入手机号");
      return;
    }
    if (commonUtil.isEmpty(params.verifyCode)) {
      commonUtil.showModal("请输入验证码");
      return;
    }
    let that = this;
    console.log("绑定手机号参数:" + JSON.stringify(params));
    commonUtil.showLoading("请稍等...");
    let openId = wx.getStorageSync("openidKey");
    wx.request({
      header: { "openId": openId,},
      url: httpsUrl + "/userCenter/bindPhone",
      dataType: "json",
      method: "POST",
      data: params,
      success: function (result) {
        commonUtil.hideLoading();
        var res = result.data;
        console.log("res:"+JSON.stringify(result));
        if (res.code == 0) {
          commonUtil.showSuccessToast("绑定成功");
          wx.navigateBack({
            delta: 1
          })
        } else {
          commonUtil.showModal(res.message);
        }
      }
    })
  },
  bindphone: function (e) {
    this.setData({
      phone: e.detail.value
    })
  },

  bindpwd: function (e) {
    this.setData({
      pwd: e.detail.value
    })
  },
  //获取验证码
  getCode() {
    var that = this
    let phone = this.data.phone
    if (phone.length != 11 || isNaN(phone)) {
      commonUtil.showModal("手机号无效");
      return;
    }
    this.setData({ codeDis: true })
    let openId = wx.getStorageSync("openidKey");
    //发送短信验证码
    wx.request({
      header: { "openId": openId, "content-Type": "application/x-www-form-urlencoded" },
      url: httpsUrl + "/common/sendSms?phone="+phone,
      dataType: "json",
      method: "GET",
      // data: { phone: phone },
      success: function (result) {
        console.info("发送验证码：" + JSON.stringify(result));
        var res = result.data;
        if (res.code == 0) {
          //发送验证码成功，显示60s，每秒减1，
          that.setData({ phoneCode: 60 })
          let time = setInterval(() => {
            let phoneCode = that.data.phoneCode
            phoneCode--
            that.setData({
              phoneCode: phoneCode
            })
            if (phoneCode == 0) {
              clearInterval(time)
              that.setData({
                phoneCode: "获取验证码",
                flag: true
              })
            }
          }, 1000)
        } else {
          commonUtil.showModal("发送失败，请稍后重试");
        }
      }
    })
  },
  bindvalidateCode(e) {
    this.setData({ validateCode: e.detail.value })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})